package com.ys.malini.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ys.malini.dao.IProductNewDao;
import com.ys.malini.model.TImages;
import com.ys.malini.model.TProduct;
import com.ys.malini.model.TProductDetail;
import com.ys.malini.web.form.ProductForm;

public class ProductNewDao extends HibernateDaoSupport  implements IProductNewDao {
	public ProductNewDao(){
		
	}

	/**
	 * 查询 新品列表
	 */
	public List<ProductForm> qryTopProduct(String classtype) {
		StringBuffer hsql = new StringBuffer(" from TProduct p,TImages i where p.productId=i.productId and p.isNew='1' and i.imageDes='正面' group by p.productId order by p.productId desc");
		ProductForm product = null;
		List<ProductForm> pdlist= new ArrayList<ProductForm>();
		//List<Object[]> list =getHibernateTemplate().find(hsql.toString());
		System.out.println("hsql=====>"+hsql);
		Query query = getSession().createQuery(hsql.toString());
		query.setFirstResult(0);
		query.setMaxResults(5);
		List<Object[]> list =query.list();
		
		TProduct p=null;
		TImages ii=null;
		if(!list.isEmpty()&&list!=null){
			
			for(int i=0;i<list.size();i++){
				product = new ProductForm();
				p = (TProduct) (((Object[]) list.get(i))[0]);
				ii =(TImages)(((Object[]) list.get(i))[1]);
				
				product.setProductId(p.getProductId()+"");
				product.setBigimages(ii.getBigimages());
				product.setMidimages(ii.getMidimages());
				product.setSeries(p.getSeries());
				product.setProductType(p.getProductType());
				
				pdlist.add(product);
				
				
				
				
			}

		}

		
		return pdlist;
	}


	/**
	 * 查询 单个新品 的图片库
	 */
	public List<TImages>  queryDetailById(String pid) {
		StringBuffer sb = new StringBuffer(" from TImages i where i.productId='"+pid+"'");
		//List<TImages> list =getHibernateTemplate().find(sb.toString());
		Query query = getSession().createQuery(sb.toString());
		query.setFirstResult(0);
		query.setMaxResults(4);
		List<TImages> list =query.list();
		
		return list;
	}

	
	/**
	 * 查询 产品的明细
	 * 
	 */

	public List<ProductForm> queryDetailInfoById(Map map) {
		String pid = (String) map.get("pid");
		String ptype = (String) map.get("ptype");
		String series =(String) map.get("series");
		
		StringBuffer sb = new StringBuffer(" from TProductDetail p,TImages i where p.productId=i.productId ");
		sb.append(" and  p.productId=p.parantProdId ");
		sb.append(" and p.productId='"+pid+"'");
		System.out.println("sb========="+sb.toString());
		ProductForm product = null;
		List<ProductForm> pdlist= new ArrayList<ProductForm>();
		List<Object[]> list =getHibernateTemplate().find(sb.toString());
		TProductDetail p=null;
		TImages ii=null;
		if(!list.isEmpty()&&list!=null){
			
			for(int i=0;i<list.size();i++){
				product = new ProductForm();
				p = (TProductDetail) (((Object[]) list.get(i))[0]);
				ii =(TImages)(((Object[]) list.get(i))[1]);
				
				product.setProductId(p.getProductId()+"");
				product.setBigimages(ii.getBigimages());
				product.setMidimages(ii.getMidimages());
				product.setProductInfo(p.getProductInfo());
				product.setParameterInfo(p.getParameterInfo());
				product.setPackInfo(p.getPackInfo());
				product.setOtherInfo(p.getOtherInfo());
				product.setSeries(p.getSeries());
				product.setMaterial(p.getMaterial());
				product.setMarketPrice(p.getMarketPrice());
				product.setSalePrice(p.getSalePrice());
				product.setBarginPrice(p.getBarginPrice());
				product.setBrand(p.getBrand());
				product.setImageDes(ii.getImageDes());
			
				
				pdlist.add(product);
				
				
			}

		}

		System.out.println("pdlist====="+pdlist.size());
		return pdlist;
	}
	
	
}
